<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>AdminLTE 3 | Data Tables</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">

                        <!--<button id="import" class="btn btn-primary btn-sm">批量导入</button>-->
                    </div>
                    <!-- /.card-header -->
                    <div class="card-body">
                        <!-- form start -->
                        <form role="form" action="" id="form_submit">
                            <div class="row">
                                <div class="col-lg-2 mt-2">
                                    <div class="">
                                        <div style=" line-height: 1.5em;margin-top: 6px;" class="text-center">
                                            <code>*</code>&nbsp;&nbsp;短信编号
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-9 mt-2">
                                    <div class="form-group">
                                        <input name="code" type="text" class="form-control" id="u_id" placeholder=""
                                               disabled>
                                        <input name="smsId" type="text" class="form-control" id="smsId" placeholder=""
                                               style="display: none">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-2 mt-2">
                                    <div class="">
                                        <div style=" line-height: 1.5em;margin-top: 6px;" class="text-center">
                                            <code>*</code>&nbsp;&nbsp;短信内容
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-9 mt-2">
                                    <div class="form-group">
                                <textarea name="content" type="text" class="form-control" id="content"
                                          style="min-height: 200px"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-2 mt-2">
                                    <div class="">
                                        <div style=" line-height: 1.5em;margin-top: 6px;" class="text-center">
                                            <code>*</code>&nbsp;&nbsp;是否全员发送
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-9 mt-2">
                                    <div>
                                        <div class="col-lg-3">
                                            <div class="input-group">
                                                <div class="input-group-prepend">
                                            <span class="input-group-text">
                                                <input name="isall" type="checkbox">
                                            </span>
                                                </div>
                                                <label class="form-control">全员发送</label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row mt-2">
                                <div class="col-lg-2">
                                    <div class="">
                                        <div style="line-height: 1.5em;margin-top: 6px;" class="text-center">
                                            <code>*</code>&nbsp;&nbsp;发送对象
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-9">
                                    <!-- /.form-group -->
                                    <div class="form-group">
                                        <select id="mobile" name="mobiles" class="form-control select2"
                                                style="width: 100%;"
                                                multiple="multiple">

                                        </select>
                                    </div>

                                </div>
                            </div>
                            <button id="save" class="btn btn-primary btn-sm">发送短信</button>

                        </form>
                    </div>
                    <!-- /.card-body -->
                </div>
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
    <footer class="main-footer text-center" style="margin-left: 0%;">
        <div class="d-none d-sm-block">
            <b>版本:</b> 1.0
        </div>
        <!-- 按钮触发模态框 -->
        <strong>Copyright &copy; 2014-2018 <a href="http://adminlte.io">咸阳恒源汽贸有限公司</a>.</strong>保留所有权.
    </footer>

    <!-- Control Sidebar -->
    <aside class="control-sidebar control-sidebar-dark">
        <!-- Control sidebar content goes here -->
    </aside>
    <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->


<!--点击删除按钮后弹出的页面-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="height: 200px">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">
                    提示
                </h4>
                <!--<button type="button" class="close" data-dismiss="modal" aria-hidden="true">-->
                <!--&times;-->
                <!--</button> -->
            </div>
            <div class="modal-body">
                信息发送成功!
                <input id="del_id" style="display: none"/>
            </div>
            <div class="modal-footer">
                <button id="delBtn" type="submit" class="btn btn-primary" data-dismiss="modal">确定</button>
                <!--<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>-->
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<!-- page script -->
<script>
    $(function () {
        $("#myModal").on("hidden.bs.modal", function () {
            $('#myModal').removeData("bs.modal");
        });

        select2fun();


        $("#importBtn").on("click", function SaveData() {
            $("#msg-content").empty();
            var name = $("#fileName").val();
            $("#importBtn").text("导入中...");
            if (name != null && name != "") {
                var url = "./batchImportData.action?type=1";
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {
                        "name": name
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.status == 200) {
                            $("#msg-content").empty();
                            var text = "<label>数据导入成功!共计导入:<label style='color: red;font-size:18px'> " + result.total + "</label></font>条数据,成功:<label style='color: red;font-size:18px'>" + result.totalSuccess + "</label>条</label>"
                            $("#msg-content").append(text);
                            $("#importBtn").attr("disabled", "disabled");
                            $("#importBtn").text("导入完成");
                        } else {
                            $("#msg-content").empty();
                            var text = "<label style='color: red;font-size:18px'>" + result.message + "</label>"
                            $("#msg-content").append(text);
                            $("#importBtn").attr("disabled", "disabled");
                            $("#importBtn").text("导入失败");
                        }


                    },
                    dataType: "json"
                });
            } else {
                $("#msg-content").empty();
                var text = "<label style='color: red;font-size:18px'>请先上传批量导入文件</label>"
                $("#msg-content").append(text);
                $("#importBtn").text("开始导入");
            }

        });

        //点击编辑图标出现编辑框
        $("#example2 tbody").unbind('click').on("click", ".edit-btn", function () {
            var id = $(this).attr("id");
            if (id != -1) {
                var url = encodeURI("./getSMSDetail.action");
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {
                        "contentId": id
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.success) {
                            //刷新表格
                            var data = result.data;
                            $("#u_id").val("FU00" + data.smsId);
                            $("#smsId").val(data.smsId);
                            $("#content").val(data.content);

                            $('#myModal').modal('show');
                        } else {

                        }
                    },
                    dataType: "json"
                });
            } else {
                $('#myModal').modal('show');
            }

        });

        //删除
        $("#example2 tbody").on("click", ".del-btn", function () {
            var id = $(this).attr("id");
            $("#del_id").val(id);

            $('#deleteModal').modal('show');
            $("#deleteModal").bind('hide.bs.modal', function () {
                $(".modal-backdrop").remove();
            });

            $("#deleteModal").on('hidden.bs.modal', '.modal', function () {
                $(this).removeData('bs.modal');
            });


        });


        $('#form_submit').bootstrapValidator({

            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'fa  fa-check',
                invalid: 'fa fa-times',
                validating: 'fa fa-refresh'
            },

            fields: {
                content: {
                    message: '校验内容失败',
                    validators: {
                        notEmpty: {
                            message: '短信内容不能为空'
                        }
                    }
                },
                mobiles: {
                    validators: {
                        notEmpty: {
                            message: '发送对象手机号码不能为空'
                        }
                    }
                }
            }

        });


        //证照属性设置保存信息
        $("#save").on("click", function SaveData() {
            $("#form_submit").bootstrapValidator('validate');//提交验证
            if ($("#form_submit").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
                var data = $('#form_submit').serialize();
                //序列化获得表单数据，结果为：user_id=12&user_name=John&user_age=20
                var submitData = decodeURIComponent(data, true);
                //submitData是解码后的表单数据，结果同上
                var url = encodeURI("./saveSmsRecord.action");
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    dataType: "json",
                    success: function (result) {
                        if (result.success) {
                            //刷新表格
                            $("#myModal").modal('show');  //手动关闭
                        } else {

                        }

                        $("#myModal").bind('hide.bs.modal', function () {
                            $(".modal-backdrop").remove();
                        });
                    },
                    dataType: "json"
                });
            }
        });

        //证照属性设置新添加信息
        $("#add").on("click", function SaveData() {
            $("#u_id").val("");
            $("#u_fid").val("");
            $("#u_name").val("");
            $("#u_position").val("");
            $("#u_school").val("");
            $("#u_tel").val("");
            $("#identityCard").val("");
            $("#u_email").val("");
            $("#u_birth").val("");
            uploadImg();
            $('#myModal').modal('show');
        });

        //证照属性设置新添加信息
        $("#import").on("click", function SaveData() {
            importFile();
            $("#importBtn").text("开始导入");
            $("#fileName").val("");
            $("#msg-content").empty();
            $("#importBtn").removeAttr("disabled");
            $('#uploadModal').modal('show');
        });

        $(".col-lg-3").on("click", function () {
            var span = $(this).find("span");
            var check = $(this).find("input:checkbox");
            var ischecked = check.prop("checked");
            check.prop("checked", !ischecked);
            if (!ischecked) {
                span.css("background-color", "rgb(254, 110, 54)");
                $("#mobile").select2().prop("disabled", true);
            } else {
                span.css("background-color", "#50a1f2");
                $("#mobile").select2().prop("disabled", false);
                select2fun();
            }
        });


        /*        $("input:checkbox").change(function () {
         var ischecked = $(this).prop("checked");
         alert(ischecked);
         if (ischecked) {
         $("#mobile").select2().prop("disabled", true);

         } else {
         $("#mobile").select2().prop("disabled", false);
         }
         });*/

    });


    function importFile() {
        $('#uploadContainer').easyUpload({
            allowFileTypes: '*.xlsx;*.xls;',//允许上传文件类型，格式'*.xlsx'
            allowFileSize: 100000,//允许上传文件大小(KB)
            selectText: '选择Excel文件',//选择文件按钮文案
            multi: false,//是否允许多文件上传
            multiNum: 1,//多文件上传时允许的文件数
            showNote: true,//是否展示文件上传说明
            note: '提示：最多上传1个文件，支持格式为xlsx.xls',//文件上传说明
            showPreview: true,//是否显示文件预览
            url: './excelfileUpload.action',//上传文件地址
            fileName: 'file',//文件filename配置参数
            formParam: {
                /*token: $.cookie('token_cookie')//不需要验证token时可以去掉*/
            },//文件filename以外的配置参数，格式：{key1:value1,key2:value2}
            timeout: 10000,//请求超时时间
            okCode: 200,//与后端返回数据code值一致时执行成功回调，不配置默认200
            successFunc: function (res) {
                console.log('成功回调', res);
                $("#msg-content").empty();
                $("#fileName").val(res.fileName);
                $("#importBtn").removeAttr("disabled");
                /*                if(res.fileName){
                 $("#pimg").attr("src",res.fileName);
                 $("#p_image").val(res.fileName);
                 }*/

            },//上传成功回调函数
            errorFunc: function (res) {
                console.log('失败回调', res);
            },//上传失败回调函数
            deleteFunc: function (res) {
                console.log('删除回调', res);
            }//删除文件回调函数
        });
    }

    function select2fun() {
        $("#mobile").select2({
            ajax: {
                type: 'GET',
                url: "./UserList.action",
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        search: params.term, // search term 请求参数 ， 请求框中输入的参数
                        page: params.page,
                        length: 10,// 每页显示多少行
                        type: 3
                    };
                },
                processResults: function (data, params) {
                    params.page = params.page || 1;
                    var itemList = [];//当数据对象不是{id:0,text:'ANTS'}这种形式的时候，可以使用类似此方法创建新的数组对象
                    var arr = data.data;
                    for (item in arr) {
                        itemList.push({id: arr[item].aid, text: arr[item].name})
                    }
                    return {
                        results: itemList,//itemList
                        pagination: {
                            more: (params.page * 10) < data.recordsTotal
                        }
                    };
                },
                cache: false
            },
            placeholder: '请选择',//默认文字提示
            language: "zh-CN",
            delay: 200,// 延迟显示
            tags: true,//允许手动添加
            allowClear: true,//允许清空
            escapeMarkup: function (markup) {
                return markup;
            }, // 自定义格式化防止xss注入
            minimumInputLength: 1,//最少输入多少个字符后开始查询
            formatResult: function formatRepo(repo) {
                return repo.text;
            }, // 函数用来渲染结果
            formatSelection: function formatRepoSelection(repo) {
                return repo.text;
            } // 函数用于呈现当前的选择
        });
    }

</script>
</body>
</html>
